package servlet;
/*
 * SavecartServlet.java
 *
 * 8:13
 */

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author HOME
 * @version
 */
public class SavecartServlet extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String itemID = request.getParameter("itemID");
        String name = request.getParameter("name");
        String imageURL = request.getParameter("imageURL");
        String type = request.getParameter("type");
        String company = request.getParameter("company");
        String scale = request.getParameter("scale");
        String quantity = request.getParameter("quantity");
        String price = request.getParameter("price");
        int lifeTime = 10000;
        
        if (quantity == null) quantity = "1";
        
        int num = Integer.parseInt(quantity);
        
        Cookie[] cookies = request.getCookies();
        
        
        boolean isFound = false;
        for(int i=0; i<cookies.length; ++i) {
            Cookie myCookie = cookies[i];
            if(myCookie.getName().equals(itemID+".ID")) {
                isFound = true;
                for(int j=0; j<cookies.length; ++j) {
                    if(cookies[j].getName().equals(itemID+".Quantity")) {
                        int s1 = Integer.parseInt(cookies[j].getValue());
                        num += s1;
                        Cookie newCookie = new Cookie(itemID+".Quantity", Integer.toString(num));
                            newCookie.setMaxAge(lifeTime);
                        response.addCookie(newCookie);
                        break;
                    }
                }
                break;
            }
        }
        if (isFound == false) {
           
            Cookie newCookie = new Cookie(itemID+".ID", itemID);
            newCookie.setMaxAge(lifeTime);
            response.addCookie(newCookie);
            newCookie = new Cookie(itemID+".Name", name);
            newCookie.setMaxAge(lifeTime);
            response.addCookie(newCookie);
            newCookie = new Cookie(itemID+".Type", type);
            newCookie.setMaxAge(lifeTime);
            response.addCookie(newCookie);
            newCookie = new Cookie(itemID+".Company", company);
            newCookie.setMaxAge(lifeTime);
            response.addCookie(newCookie);
            newCookie = new Cookie(itemID+".imageURL", imageURL);
            newCookie.setMaxAge(lifeTime);
            response.addCookie(newCookie);
            newCookie = new Cookie(itemID+".Scale", scale);
            newCookie.setMaxAge(lifeTime);
            response.addCookie(newCookie);
            newCookie = new Cookie(itemID+".Quantity", quantity);
            newCookie.setMaxAge(lifeTime);
            response.addCookie(newCookie);
            newCookie = new Cookie(itemID+".Price", price);
            newCookie.setMaxAge(lifeTime);
            response.addCookie(newCookie);            
        }
        response.sendRedirect("savecart.jsp?name=" +name);
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
